/**
 * $Revision: 1.0 $
 * $Author: Eric Yang $
 * $Date: Nov 22, 2009 5:44:07 PM $
 *
 * Author: Eric Yang
 * Date  : Nov 22, 2009 5:44:07 PM
 *
 */
package com.gooex.web.action.gbuy.common;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.c4j.string.StringTools;

import com.gooex.domain.base.Category;
import com.gooex.domain.base.Order;
import com.gooex.domain.base.Product;
import com.gooex.domain.base.TempCategory;
import com.gooex.domain.common.Region;
import com.gooex.service.base.IOrderManager;
import com.gooex.service.base.IProductManager;
import com.gooex.web.action.BaseAnonymousAction;


/**
 * @author Eric Yang
 * @version 1.0
 */
public class SearchAction extends BaseAnonymousAction {

	private static final Log logger = LogFactory.getLog(SearchAction.class);

	private IProductManager productManager;

	private IOrderManager orderManager;

	private List<Product> products;

	private List<Order> orders;

	private List<TempCategory> categories;

	private Category category;

	private String type;

	private String key;

	private String cateId;

	private int cateLevel = 1;

	public String execute() throws Exception {
		if (StringTools.isNotBlankOrNull(key)) {
			key = URLDecoder.decode(key.trim(), "UTF-8");
			key = key.trim();
		}

		if ("product".equals(type)) {
			if (StringTools.isNotBlankOrNull(key))
				products = productManager.findProductsByName(key, page);
			return "product";
		} else {
			if (StringTools.isNotBlankOrNull(key) && key.toLowerCase().matches("[a-z0-9]{20}")) {
				return "viewOrder";
			}
			category = categoryManager.getCategory(cateId);
			List<String> keys = splitKey(key);

			String regionId = null;
			Region region = getCurrentRegion();
			if (region != null)
				regionId = region.getId();
			orders = orderManager.findOrderByTitle(keys, cateId, regionId, page);
			categories = orderManager.getCategoryCount(cateId, key, regionId, cateLevel);
			return "order";
		}
	}

	private List<String> splitKey(String key) {
		if (key == null)
			return null;
		String[] keys = key.split(" ");
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < keys.length; i++) {
			String[] items = keys[i].split("/");
			list.addAll(Arrays.asList(items));
		}
		for (int i = list.size() - 1; i > 0; i--) {
			String s = list.get(i);
			if (StringTools.isBlankOrNull(s))
				list.remove(i);
		}
		return list;
	}

	public void setProductManager(IProductManager productManager) {
		this.productManager = productManager;
	}

	public List<Product> getProducts() {
		return products;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public List<Order> getOrders() {
		return orders;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setOrderManager(IOrderManager orderManager) {
		this.orderManager = orderManager;
	}

	public String getCateId() {
		return cateId;
	}

	public void setCateId(String cateId) {
		this.cateId = cateId;
	}

	public List<TempCategory> getCategories() {
		return categories;
	}

	public int getCateLevel() {
		return cateLevel;
	}

	public void setCateLevel(int cateLevel) {
		if (cateLevel > 3)
			cateLevel = 3;
		this.cateLevel = cateLevel;
	}

	public Category getCategory() {
		return category;
	}

}
